// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:49:42
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Dynamic\EIGRLParser.cpp
 */

#include "Import/BDFImport/BulkData/Dynamic/EIGRLParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/IEIGRLService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (EIGRL);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/EIGRL/EIGRL.md>
*/

// 以下需要修改
enum EIGRLIndex : std::uint8_t
{
    EIGRLIndex_SID = 1,
    EIGRLIndex_V1  = 2,     // For vibration analysis: frequency range of interest. For buckling analysis: eigenvalue
                            // range of interest. See Remark  4. (Real or blank,  )
    EIGRLIndex_V2     = 3,
    EIGRLIndex_ND     = 4,  // Number of roots desired. See Remark  4. (Integer > 0 or blank)
    EIGRLIndex_MSGLVL = 5,  // Diagnostic level. (0 < Integer < 4; Default = 0)
    EIGRLIndex_MAXSET = 6,  // Number of vectors in block or set. Default is machine dependent. See Remark  14.
    EIGRLIndex_SHFSCL = 7,  // Estimate of the first flexible mode natural frequency. See Remark  10. (Real or blank)
    EIGRLIndex_NORM   = 8   // Method for normalizing eigenvectors (Character: “MASS” or “MAX”)
                            // MASS  Normalize to unit value of the generalized mass. Not available for
                            // buckling analysis. (Default for normal modes analysis.)
                            // MAX  Normalize to unit value of the largest displacement in the analysis set.
                            // Displacements not in the analysis set may be larger than unity. (Default
                            // for buckling analysis.)
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseEIGRL (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::EIGRLData tempEIGRL;

    // TODO(bug 001) 未考虑NORM=MAX NUMS=2这种情况
    // TODO(bug 001) m_dominId的正确填写
    tempEIGRL.m_domainId   = 1;
    bool        isHaveNORM = false;  // 判断NORM是否有值，没有则默认MASS
    std::string curStrKey;
    for (size_t i = 1; i < 9; i++)
    {
        curStrKey          = dividedEntryInfo[i];
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            continue;
        }

        switch (i)
        {
        case EIGRLIndex_SID:
            tempEIGRL.m_sId = convertToI64 (curStrKey);
            break;
        case EIGRLIndex_V1:
            tempEIGRL.m_v1    = convertToReal (curStrKey);
            tempEIGRL.m_FLAG1 = 0;
            break;
        case EIGRLIndex_V2:
            tempEIGRL.m_v2    = convertToReal (curStrKey);
            tempEIGRL.m_FLAG2 = 0;
            break;
        case EIGRLIndex_ND:
            tempEIGRL.m_ND = convertToI64 (curStrKey);
            break;
        case EIGRLIndex_MSGLVL:
            tempEIGRL.m_MSGLVL = convertToI64 (curStrKey);
            break;
        case EIGRLIndex_MAXSET:
            tempEIGRL.m_MAXSET = convertToI64 (curStrKey);
            break;
        case EIGRLIndex_SHFSCL:
            tempEIGRL.m_SHFSCL = convertToReal (curStrKey);
            break;
        case EIGRLIndex_NORM:
            boost::trim (curStrKey);
            isHaveNORM = true;
            std::strncpy (tempEIGRL.m_NORM, curStrKey.c_str (), 4);
            break;
        default:
            break;
        }
    }

    if (!isHaveNORM)
    {
        std::string str = "MASS";
        std::strncpy (tempEIGRL.m_NORM, str.c_str (), 4);
    }

    return dbServiceFactory->get<IEIGRLService> ()->Add (tempEIGRL);
}